﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Rose.Models;
using Rose.Helper;


namespace Rose.Controllers
{
    public class AlbumController : Controller
    {
        //
        // GET: /Album/
        private Database_RoseEntities db = new Database_RoseEntities();

        public ActionResult Index()
        {
            return View();
           
        }

        public ActionResult Index1()
        {
            return View();
        }
      
        public ActionResult UploadPhoto(string albumId)
        {
            return View();
            ///使用iframe来局部化
            ///

        }


        #region 查看所有相册 Json
        //查看所有相册

        //[HttpPost]
        //public ActionResult GetAlbumNameJson()
        //{
        //    string selfId = new CheckHelpModel().CheckCurrentUserId();
        //    return Json(new ListHelpModel().GetAlbumList(selfId), JsonRequestBehavior.AllowGet);

        //}


        //public ActionResult AlbumPhotoJson(string albumId)
        //    //只有自己可见
        //{
        //    string selfId = new CheckHelpModel().CheckCurrentUserId();

        //    List<PhotoModel> returnModel = new AlbumHelpModel().GetAlbumPhotos(albumId,selfId);


        //    return Json(returnModel, JsonRequestBehavior.AllowGet);
        //}
        public ActionResult AlbumPhotoJson(string albumId)
        { 
            CheckHelpModel checkhelp=new CheckHelpModel();
            string selfId = checkhelp.CheckCurrentUserId();
          
            Album album = checkhelp.CheckAlbum(albumId);
            string albumMasterId = album.UserId;

            List<PhotoModel> returnModel = new List<PhotoModel>();

            if (album.IsOnlyForFriend == false ||albumMasterId==selfId)
            {
                returnModel = new AlbumHelpModel().GetAlbumPhotos(albumId,albumMasterId);
                return Json(returnModel,JsonRequestBehavior.AllowGet);
            }
            CheckRelationHelpModel relationcheck=new CheckRelationHelpModel();

            if(album.IsOnlyForFriend==true && relationcheck.IsFriendRelation(albumMasterId,selfId))
            {
                  returnModel = new AlbumHelpModel().GetAlbumPhotos(albumId,albumMasterId);
                return Json(returnModel,JsonRequestBehavior.AllowGet);
            }
             
                return Json(returnModel,JsonRequestBehavior.AllowGet);
        }


    

        public ActionResult AllPhotoJson(string friendId)
        {
            string selfId = new CheckHelpModel().CheckCurrentUserId();

            if (selfId == friendId)
            {
                IEnumerable<Album> albumlist = db.Albums.Where(c => c.UserId == selfId);
                List<PhotoModel> returnModle = new List<PhotoModel>();
                foreach (var album in albumlist)
                {
                    returnModle.AddRange(new AlbumHelpModel().GetAlbumPhotos(album.Id.ToString(), selfId));
                }
                return Json(returnModle, JsonRequestBehavior.AllowGet);
            }

            bool isFriend=new CheckRelationHelpModel().IsFriendRelation(selfId,friendId);

            if (isFriend)
            {
                IEnumerable<Album> albumlist = db.Albums.Where(c => c.UserId == friendId && c.IsOnlyForFriend != null);


                List<PhotoModel> returnModle = new List<PhotoModel>();
                foreach (var album in albumlist)
                {
                    returnModle.AddRange(new AlbumHelpModel().GetAlbumPhotos(album.Id.ToString(), friendId));
                }
                return Json(returnModle, JsonRequestBehavior.AllowGet);
            }
            else
            {
                IEnumerable<Album> albumlist = db.Albums.Where(c => c.UserId == friendId && c.IsOnlyForFriend == false);
                List<PhotoModel> returnModle = new List<PhotoModel>();
                foreach (var album in albumlist)
                {
                    returnModle.AddRange(new AlbumHelpModel().GetAlbumPhotos(album.Id.ToString(), friendId));
                }
                return Json(returnModle, JsonRequestBehavior.AllowGet);
            }
            



        }

        //其实是除了隐私的部分外任何人都可以看见
        //public ActionResult PublicAllPhotoJson(string friendId)
        //{
           
        //}
      //返回对公众开放的相册;

        #endregion

       

        #region  part

        [Authorize]
        public ActionResult AlbumListPart()
        {
            string selfId = new CheckHelpModel().CheckCurrentUserId();
            var returnModel =new  AlbumHelpModel().GetAlbumModels(selfId, null);
            return View(returnModel);
        }


        [Authorize]
        public ActionResult NewAlbumPart()
        {
            return View(new AlbumModel());
        }


        [Authorize]
        public ActionResult UploadPhotosPart(string albumId)
        {
            string selfId = new CheckHelpModel().CheckCurrentUserId();
            ViewData["albumId"] = albumId;
            
            return View();
        }

        #endregion



        [HttpPost]
        [Authorize]
        public ActionResult AddAlbum(string title,int isonlyforfriend)
        {
            AlbumModel model = new AlbumModel()
                {
                    Title=title,
                    IsOnlyForFriend=isonlyforfriend
                };

            string selfId = new CheckHelpModel().CheckCurrentUserId();

            string albumId=new AlbumHelpModel().CreateAlbum(model,selfId);

            return Content(albumId);
        }


        #region 修改相册

        [HttpPost]
        [Authorize]

        public ActionResult ChangeAlbum(string title, int isOnlyForFriend,string albumId)
        {
            string selfId = new CheckHelpModel().CheckCurrentUserId();
            if (new AlbumHelpModel().ChangeAlbum(title, isOnlyForFriend, selfId,albumId))
                return Content("修改成功");
            return Content("修改失败");
        }

        //[HttpPost]
        //[Authorize]
        //public ActionResult ChangeLimit(string albumId, int isOnlyForFriend)
        //{
        //    string selfId = new CheckHelpModel().CheckCurrentUserId();
        //    if ( new AlbumHelpModel().ChangeAlbumLimit(albumId, selfId, isOnlyForFriend))
        //        return Content("修改成功");
        //    return Content("修改失败");
        //}


        //[HttpPost]
        //[Authorize]
        //public ActionResult ChangeImagePath(string albumId, string path)
        //{
        //    string selfId = new CheckHelpModel().CheckCurrentUserId();
        //    if ( new AlbumHelpModel().ChangeAlbumImagePath(albumId, selfId, path))
        //        return Content(path);
        //    return Content("");
        //}



        [HttpPost]
        [Authorize]
        public ActionResult ChangePhotoTitle(string photoId, string title)
        {

            string selfId = new CheckHelpModel().CheckCurrentUserId();
            if (new AlbumHelpModel().ChangePhotoTitle(selfId,photoId, title))
                return Content(title);
            return Content("修改标题为"+title+"时发生错误");
        }
        #endregion

        #region delete
        
        [Authorize]
        [HttpPost]
        public ActionResult DeletePhoto(string albumId, string photoId)
        {
            string selfId = new CheckHelpModel().CheckCurrentUserId();
            if ( new AlbumHelpModel().DeletePhotoFromDatabase(selfId, albumId, photoId))
                return Content("删除相片成功");
            return Content("删除相片失败");
        }


        [Authorize]
        [HttpPost]
        public ActionResult DeleteAlbum(string albumId)
        {
            string selfId = new CheckHelpModel().CheckCurrentUserId();
            if (new AlbumHelpModel().DeleteAlbumFromDatabase(selfId, albumId))
                return Content("删除相册成功");
            return Content("删除相册失败");
        }


        

        #endregion


    }
}
